﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using KINLO.Data.Reporitory;
using KINLO.Model.Dto;
using KINLO.UI.Main.View;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace KINLO.UI.Main.VM
{
    public class FrontWeightClearVM : ViewModelBase
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(FrontWeightClearVM));
        FrontWeightClearView View;
        WeightReporitory DB = new WeightReporitory();
        FrontWeightReporitory FDB = new FrontWeightReporitory();

        public FrontWeightClearVM(FrontWeightClearView view)
        {
            this.View = view; 
        }


        bool allSelected;
        public bool AllSelected
        {
            get { return allSelected; }
            set
            {
                allSelected = value;
                RaisePropertyChanged("AllSelected");
            }
        }

        RelayCommand<DataGridRowEventArgs> cmdLoadingRow;
        public RelayCommand<DataGridRowEventArgs> CmdLoadingRow
        {
            get { return cmdLoadingRow ?? (cmdLoadingRow = new RelayCommand<DataGridRowEventArgs>((e) => DoCmdLoadingRow(e))); }
        }
        void DoCmdLoadingRow(DataGridRowEventArgs e)
        {
            e.Row.Header = e.Row.GetIndex() + 1;
        }
        ObservableCollection<WeightDto> weightDtos;
        public ObservableCollection<WeightDto> FrontWeights
        {
            get { return weightDtos; }
            set
            {
                weightDtos = value;
                RaisePropertyChanged("FrontWeights");
            }
        }

        /// <summary>
        /// 事件选择
        /// </summary>
        RelayCommand cmdCbkSelect;
        public RelayCommand CmdCbkSelect
        {
            get { return cmdCbkSelect ?? (cmdCbkSelect = new RelayCommand(() => DoCmdCbkSelect())); }
        }
        void DoCmdCbkSelect()
        {
            if (FrontWeights != null && FrontWeights.Count > 0)
            {
                foreach (var evt in FrontWeights)
                {
                    evt.IsChecked = AllSelected;
                }
            }
        }
        /// <summary>
        /// 事件查询
        /// </summary>
        RelayCommand cmdEventQuery;
        public RelayCommand CmdEventQuery
        {
            get { return cmdEventQuery ?? (cmdEventQuery = new RelayCommand(() => DoCmdEventQuery())); }
        }
        void DoCmdEventQuery()
        {
            var frontWeights = DB.GetBattryInfoByFrontSegment2();
            if (frontWeights != null)
            {
                FrontWeights = new ObservableCollection<WeightDto>(frontWeights);
            }
        }

        /// <summary>
        /// 清除记录
        /// </summary>
        RelayCommand cmdClear;
        public RelayCommand CmdClear
        {
            get { return cmdClear ?? (cmdClear = new RelayCommand(() => DoCmdClear())); }
        }
        void DoCmdClear()
        {
            try
            {
                if (MessageBox.Show("确定删除", "提示", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
                {
                    for (var i = 0; i < FrontWeights.Count; i++)
                    {
                        var fw = FrontWeights[i];
                        if (fw.IsChecked == true)
                        {
                            FDB.DeleteFrontWeight(fw.BattBarCode);
                            DB.DeleteBattryInfo(fw.ID);
                        }
                    }

                    DoCmdEventQuery();
                }
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
        }

        /// <summary>
        /// 退出
        /// </summary>
        RelayCommand cmdExit;
        public RelayCommand CmdExit
        {
            get { return cmdExit ?? (cmdExit = new RelayCommand(() => DoCmdExit())); }
        }
        void DoCmdExit()
        {
            try
            {
                this.View.Close();
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
        }
    }
}
